package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * @name 沈阳律师网
 * @link http://www.024lawyer.com/
 * @author dhy
 */ 
public class _024lawyerNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.024lawyer.com/news_view.asp\\?newsid=[0-9]*",
		"http://www.024lawyer.com/newsshow.php\\?cid=[0-9]*&id=[0-9]*"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title=doc.select("font[size=5] b").text();
		if(StringUtils.isBlank(title))
			title=doc.select("font[color=#000080]").text();
		if(StringUtils.isBlank(title))
			title=doc.select(".title").text();
			
		author = JSoupUtils.matchAuthor(doc, "来源：");
			
		date=DateUtils.matchDate(doc.select("center").text());
		if(date==null)
			date=DateUtils.matchDate(doc.select("#textarea").text());
		
		content =HtmlCleaner.getContentHtml(url, doc.select("html body div table#table1 tbody tr td"));
		if(StringUtils.isBlank(content))
			content =HtmlCleaner.getContentHtml(url, doc.select("td.news-content"));
		if(StringUtils.isBlank(content))
			content =HtmlCleaner.getContentHtml(url, doc.select("#textarea p"));
		content = content.replaceAll("<[a-z\\/\\ ]+>", "");
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	public static void main(String[] args) {
		_024lawyerNewsAnalyse analyse=new _024lawyerNewsAnalyse();
		String url="http://www.024lawyer.com/newsshow.php?cid=51&id=105";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}

	
